package controllers.power.admin;

import controllers.BaseController;
import controllers.auth.Secure;
import controllers.auth.SkipAuth;
import models.admin.AdminDept;
import models.constants.DeletedStatus;
import play.mvc.Controller;
import play.mvc.With;
import util.common.ConvertUtil;

import java.util.*;

/**
 * 部门管理
 * Created by youliangcheng on 17/4/8.
 */
@With(BaseController.class)
public class AdminDeptCtrl extends Controller {
    @SkipAuth
    public static void adminDeptList() {
        render();
    }

    //加载数据,默认展开两层
    public static void loadTreeData(long id) {
        List<AdminDept> adminDeptList = new ArrayList<>();
        if(id == 0){
            List<AdminDept> deptList = AdminDept.loadChildAdmnDept(id);
            if(deptList != null){
                for(AdminDept adminDept : deptList){
                    adminDeptList.add(adminDept);
                    List<AdminDept> childList = AdminDept.loadChildAdmnDept(adminDept.id);
                    adminDeptList.addAll(childList);
                }
            }

        }else {
            adminDeptList = AdminDept.loadChildAdmnDept(id);
        }

        renderJSON(ConvertUtil.toJson(adminDeptList));
    }

    public static void saveDeptDetail(AdminDept adminDept) {
        if (adminDept != null && adminDept.id != null) {
            adminDept.update();
        } else if (adminDept != null) {
            //自动生成编号
            adminDept.loadKey();
            //叶子节点
            adminDept.isLeaf = true;
            adminDept.deleted = DeletedStatus.UN_DELETED;
            //创建人
            adminDept.ctUserId = Secure.getLoginUserId();
            adminDept.createAt = new Date();
            adminDept.save();
            //判断父节点是否存在,
            AdminDept parentDept;
            Long parentId = adminDept.parentId;
            parentDept = AdminDept.findBydeptId(parentId);
            if (parentId != null && parentId > 0) {
                //判断父节点是否是叶子节点
                if (parentDept != null && parentDept.isLeaf == true) {
                    parentDept.isLeaf = false;
                    parentDept.save();
                }
                //节点路径
                if (adminDept != null && adminDept.parentId > 0) {
                    adminDept.nodeIdPath = parentDept.nodeIdPath + "/" + adminDept.getId() + "/";
                } else {
                    adminDept.nodeIdPath = "/" + adminDept.getId() + "/";
                }
                adminDept.save();
            }
        }
        flash.put("msg", "保存成功!");
        redirect("/power/admin/AdminDeptCtrl/adminDeptList");

    }

    public static void deletedDept(long id) {

        AdminDept adminDept = AdminDept.findBydeptId(id);
        if(adminDept == null){
            flash.put("msg","删除失败,您要删除的数据可能已经删除!");
        }else if(adminDept.isLeaf != true){
            flash.put("msg","删除失败,请先删除子节点,再删除本节点!");
        }else if(adminDept.checkIsUsed(id)){
            flash.put("msg","删除失败,您要删除的数据正在被使用!");
        }else {
            adminDept.deleteAdminDept(id);
            flash.put("msg","删除成功!");
        }

        redirect("/power/admin/AdminDeptCtrl/adminDeptList");

    }

    @SkipAuth
    public static void popDeptList(){
        render();
    }
}
